package com.dongdong.bookstore.module.common.service.impl;

import com.dongdong.bookstore.common.service.RedisService;
import com.dongdong.bookstore.module.common.service.UploadCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @author chiangkai
 * @date 2021-11-05
 * @email chiangkk@163.com
 */
@Service
public class UploadCacheImpl implements UploadCache {
    @Autowired
    private RedisService redisService;

    @Value("${redis.database}:${redis.key.common.uploads}")
    private String prefix;

    @Override
    public Set<Integer> getUploadSlice(String key) {
        String KEY = prefix + ":fileHash:" + key;
        Object o = redisService.get(KEY);
        if (o == null) {
            return new HashSet<Integer>();
        } else {
            return (HashSet<Integer>) o;
        }
    }

    @Override
    public void setUploadSlice(String key, Set<Integer> lists) {
        String KEY = prefix + ":fileHash:" + key;
        redisService.set(KEY, lists);
    }

    @Override
    public void removeUploadSlice(String key) {
        String KEY = prefix + ":fileHash:" + key;
        redisService.del(KEY);
    }
}
